===================================================================== 
       Apple-II To IBM-PC Disk Transfer Program (AP2222PC.SEQ)
--------------------------------------------------------------------- 
  Edited 2016 (for personal use) by: James Davis, Beagle Buddy #227.===================================================================== 

\\ ****************************************************************** 

   apple <-> pc disk and sequential file transfer program

                                                  Clay Chang
                                                  Taipei,Taiwan,ROC
                                                  Oct-1994

{

\ global routine

  : .help
    cr ." apple <-> pc disk and sequential file transfer utility"
    cr ." Clay Chang,Taipei,Taiwan,ROC Oct-1994"
    cr
    cr ." useage:"
    cr ." ap2222pc -<option> filename"
    cr
    cr ." where option is:"
\   cr ." -0 test line connection"
    cr ." -1 apple disk -> pc .dsk file"
    cr ." -2 pc .dsk file -> apple disk"
    cr ." -3 sequential file apple -> pc"
    cr ." -4 sequential file pc -> apple"
    cr
    cr ." example: ap2222pc -1 ultima0.dsk"
    cr ;

  defer -abort
  handle fileh
  : esc?
    bdoskey? 27 = if -abort then ;
             
\ command line interpret

  0 value opt
  : tryopen
    opt ascii 2 = opt ascii 4 = or
    if
      fileh hopen 0<>
      if cr beep ." file now found!!"
      else fileh hclose drop then
    then ;
  : initcmd

===================================================================== 
                               Page 1
===================================================================== 
       Apple-II To IBM-PC Disk Transfer Program (AP2222PC.SEQ)
===================================================================== 

    dos_to_tib
    bl word 1+ c@ ascii - =
       here 2+ c@ ascii 0 ascii 4 between
    and if here 2+ c@ !> opt
    else cr beep ." option error" .help abort
    then
    bl word c@ 0=
    if cr beep ." invalid filename" abort then
    here fileh $>handle tryopen ;

\ parallel port i/o routine

  0 value ip#               \ status port as input
  0 value op#               \ data port as output
  : p> ip# p@ ;
  : >p op# p! ;
  : seekport 0 $408 @l dup 0=
    if cr beep drop ." no parallel port present!" abort
    else dup !> op# 1+ !> ip# then ;
  : gethski  p> $40 and $40 / ;  \ get bit 6 value
  : hski1? begin esc? gethski    until ;
  : hski0? begin esc? gethski 0= until ;
  : hsko0  0 >p ;
  : getd p> $38 and 8  /  1 >p ;
  : putd 8 or >p ;
  : @3bits hski1? getd hski0? hsko0 ;
  : !3bits hski1? putd hski0? hsko0 ;
  : @byte @3bits $20 * @3bits 4 * @3bits 2 / + + ;
  : !byte dup $40 / !3bits dup 8 / !3bits !3bits ;

\ memory allocation ... 35k buffer

  35 1024 * constant bsize
  0 value bseg
  variable bidx
  variable bmax
  : initbuf bsize paragraph alloc 0=
    if !> bseg 0 bidx ! 0 bmax ! drop
    else cr 2drop beep ." memory allocation error!" abort
    then ;
  : overbuf bseg dealloc drop ;
  : >b bseg bidx @ c!l 1 bidx +! ;
  : b> bseg bidx @ c@l 1 bidx +! ;
  : b>f 0 bidx @ fileh bseg exhwrite 0=
    if cr beep ." warning! write nothing!" -abort
    else 0 bidx ! then ;
  : f>b 0 bsize  fileh bseg exhread bmax ! 0 bidx ! ;

===================================================================== 
                               Page 2
===================================================================== 
       Apple-II To IBM-PC Disk Transfer Program (AP2222PC.SEQ)
===================================================================== 

  : full? bidx @ bsize = if b>f then ;
  : empt? bidx @ bsize = if f>b then ;

\ dos file handler words & command line interpret

  : newfile
    read-write
    fileh hcreate 0<>
    if cr beep ." can't creat a new file!" overbuf abort then ;
  : openfile
    read-only
    fileh hopen 0<>
    if cr beep ." file not found!" overbuf abort then ;
  : closefile fileh hclose
    0<> if cr beep ." warning! file don't close properly!" then ;
  : endnewfile 0 0 fileh hwrite drop closefile ;
  
\ checksum routine

  variable checksum
  : check! dup checksum @ xor checksum ! ;
  : .check cr ." the checksum is: " checksum @ . ;

\ init,end and abort program
  : init initcmd seekport initbuf hsko0 0 checksum !
    cr ." now run and follow instruction of apple side program"
    cr ." DOITNOW!
    key drop
    cr ." now working, press ESC key anytime to abort this program" ;
  : overprog overbuf .check
    cr ." see you next time!" ;
  : --abort cr beep ." job didn't done!" overbuf closefile abort ;
' --abort is -abort

\ 0.connection checking routine

  : (f0) ;
  : f0 ;

\ 1.apple -> pc disk transfer

  : (f1)
    cr ." apple -> pc disk transferring now"
    4 0 do
    begin esc? @byte check! >b bsize bidx @ = until
    b>f loop ;
  : f1 newfile (f1) endnewfile ;

===================================================================== 
                               Page 3
===================================================================== 
       Apple-II To IBM-PC Disk Transfer Program (AP2222PC.SEQ)
===================================================================== 

\ 2.pc -> apple disk transfer

  : toosmall? bmax @ bsize <>
    if cr beep ." too small sized disk image!" -abort then ;
  : (f2)
    cr ." pc -> apple disk transferring now"
    4 0 do f>b toosmall?
    begin esc? b> check! !byte bsize bidx @ = until
    loop ;
  : f2 openfile (f2) closefile ;

\ 3.apple -> pc sequential file transfer

  : ?0?-3       0= if true else false then ;
  : ?cr?-3 dup $0d = if dup >b full? $0a >b full? then ;
  : (f3)
    cr ." apple -> pc file transferring now"
    begin esc?
      @byte check!
      dup $20 <
      if ?cr?-3 ?0?-3
      else >b full? false
      then
    until
    bidx @ 0<> if b>f then ;
  : f3 newfile (f3) endnewfile ;

\ 4.pc -> apple sequential file transfer

  : ?cr?-4 dup $0d = if dup !byte empt? b> drop then ;
  : ?0?-4 drop bmax @ bidx @ = if 0 !byte true else false then ;
  : (f4)
    cr ." pc -> apple file transferring now"
    f>b begin esc?
          empt? b>
          check!
          dup $20 <
          if ?cr?-4 ?0?-4
          else !byte false then
        until ;
  : f4 openfile (f4) closefile ;

\ function selector

  : selfcn opt
    case
      ascii 0 of f0 endof

===================================================================== 
                               Page 4
===================================================================== 
       Apple-II To IBM-PC Disk Transfer Program (AP2222PC.SEQ)
===================================================================== 

      ascii 1 of f1 endof
      ascii 2 of f2 endof
      ascii 3 of f3 endof
      ascii 4 of f4 endof
      drop
    endcase ;

\ main entry point

: main
  init selfcn overprog ;

===================================================================== 



































===================================================================== 
                               Page 5
